package com.etc.baolema.dao.impl;

import com.etc.baolema.dao.UserDao;
import com.etc.baolema.entity.User;
import com.etc.baolema.util.DataSourceUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public int ADDUser(User user) {
        Connection conn = DataSourceUtil.getConnection();
        PreparedStatement ps =null;
        String sql  ="insert into tab_user(uid,user,upwd,uname,phone,uimg,birth,state,intergral) values(null,?,?,?,?,?,?,?,0)";
        int result =-1;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUser());
            ps.setString(2,user.getUpwd());
            ps.setString(3,user.getUname());
            ps.setString(4,user.getPhone());
            ps.setString(5,user.getUimg());
            ps.setString(6,user.getBirth());
            ps.setString(7,user.getState());
            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtil.releaseResource(ps,conn);
        }
        return result;
    }

    @Override
    public User login(String user) {
        String sql = "select * from tab_user where user=?";
        Connection conn = DataSourceUtil.getConnection();
        PreparedStatement ps=null;
        ResultSet rs = null;
        User u=null;
        try {
            ps= conn.prepareStatement(sql);
            ps.setString(1,user);
            rs=ps.executeQuery();
            if(rs.next()){
                u=new User();
                u.setUid(rs.getInt(1));
                u.setUser(rs.getString(2));
                u.setUpwd(rs.getString(3));
                u.setUname(rs.getString(4));
                u.setPhone(rs.getString(5));
                u.setUimg(rs.getString(6));
                u.setBirth(rs.getString(7));
                u.setState(rs.getString(8));
                u.setIntergral(rs.getString(9));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;
    }
}
